package com.aloha.common.event;

import com.aloha.common.aop.SysLog;
import com.aloha.po.User;
import com.aloha.springframework.beans.BeansException;
import com.aloha.springframework.beans.factory.annotation.Autowired;
import com.aloha.springframework.context.ApplicationContext;
import com.aloha.springframework.context.ApplicationContextAware;
import com.aloha.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

/**
 * @author DaiZhiHeng
 * @date 2023/7/26 19:58
 */
@Slf4j
@Component
public class UserService implements ApplicationContextAware {

    @Autowired
    private UserContext userContext;

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void register(String id) {
        log.info("开始注册用户：{}", id);
        applicationContext.publishEvent(new UserRegisterEvent(applicationContext, userContext.getUser(id)));
    }

    @SysLog
    public User queryUser(String uid) {
        return userContext.getUser(uid);
    }
}

